<?php

namespace App\Http\Controllers\Api;


use App\Models\Cartoon;
use App\Models\CartoonMembers;
use App\Models\CartoonRoles;
use App\Models\Member;
use App\Models\Sms;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Validate\api\CartoonValidate;

class CartoonController extends BaseController
{
    protected $Cartoon;
    protected $v;
    protected $Sms;
    /**
     *
     * @param Request $request
     * @return array|\Illuminate\Http\JsonResponse
     */

    public function __construct(Cartoon $Cartoon,CartoonValidate $cartoonValidate,Sms $Sms)
    {
        $this->Cartoon = $Cartoon;
        $this->v = $cartoonValidate;
        $this->Sms = $Sms;
    }


    /**
     * api获取列表信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList(Request $request)
    {

        $member_id = Auth::guard('api')->user()->member_id;
//        $id=1;
        $where['status']=1;
//        $where['member_id']=$member_id;
        if (!empty($request->name)){
            $where[]=['name','like','%'.$request->name.'%'];
        }
        $user = $this->Cartoon->getList($where,$request->pageSize);

        $data['banner']='http://';
        $data['total']=$user['total'];
        $data['list']=$user['data'];


        if (empty($user['data'])){
            return response()->json(['code'=>1,'msg'=>'没有数据了','data' => $data]);
        }else{
            return response()->json(['code'=>1,'msg'=>'成功','data' => $data]);
        }

    }

    /**
     * 添加漫画项目
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCartoon(Request $request)
    {

        $member_id = Auth::guard('api')->user()->member_id;
//        $member_id=1;
        $data=$request->input();
        $this->v->validate('addCartoon');
        $rs =$this->Cartoon->create([
            'member_id'=> $member_id,
            'name'=>   $data['name'],
            'img'=> $data['img'],
            'text'=>$data['text'],
            'label'=>$data['label'],
        ]);
        $CartoonMembers=new CartoonMembers();
        $rs2 =$CartoonMembers->create([
            'member_id'=> $member_id,
            'is_c'=> 1,
            'cartoon_id'=>   $rs->id,
        ]);
        if ($rs&&$rs2){
            return response()->json(['code'=>1,'msg'=>'创建成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'创建失败']);
        }
    }
    /**
     * 修改漫画项目
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveCartoon(Request $request){
        $data = $request->all();
        $this->v->validate('addCartoon');
            $up['name']=$data['name'];
            $up['img']=$data['img'];
            $up['text']=$data['text'];
//            $up['label']=$data['label'];

//        $member_id=Auth::user()->member_id;
        $member_id=1;
        $cartoon = $this->Cartoon->where(['member_id'=>$member_id,'id'=>$data['id']])->value('id');
        if (empty($cartoon)){
            return response()->json(['code'=>0,'msg'=>'当前用户不能修改本漫画']);
        }
        $rs = $this->Cartoon->where(['member_id'=>$member_id,'id'=>$data['id']])->update($up);
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);
        }
    }


    /**
     * 删除漫画项目
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delCartoon(Request $request){
        $data = $request->all();

        $member_id=Auth::user()->member_id;
        $member_id=1;
        $cartoon = $this->Cartoon->where(['member_id'=>$member_id,'id'=>$data['id']])->value('name');
//        if (empty($cartoon)){
//            return response()->json(['code'=>0,'msg'=>'当前用户不能删除本漫画']);
//        }
        $up['status']=2;
        $rs = $this->Cartoon->where(['id'=>$data['id']])->update($up);
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    /**
     * 漫画项目详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCartoonInfo(Request $request){
        $data = $request->all();
        $cartoon_info = $this->Cartoon->find([$data['id']]);
        if (!empty($cartoon_info[0])){
            return response()->json(['code'=>1,'msg'=>'操作成功','data'=>$cartoon_info[0]]);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    /**
     * 添加项目成员
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCartoonMembers(Request $request){
        $data = $request->all();
        $this->v->validate('addCartoonMembers');

        $member_id=Auth::user()->member_id;
        $Member=new Member();

        $to_member = $Member->getData(['phone'=>$data['phone']]);
        if (empty($to_member)){
            return response()->json(['code'=>0,'msg'=>'该用户尚未注册']);
        }
        $CartoonMembers=new CartoonMembers();
        $to_CartoonMembers = $CartoonMembers->getData(['member_id'=>$to_member->member_id,'cartoon_id'=>$data['cartoon_id']]);
        if (!empty($to_CartoonMembers)){
            return response()->json(['code'=>0,'msg'=>'请勿重复添加']);
        }
        $cartoon = $CartoonMembers->getData(['member_id'=>$member_id,'cartoon_id'=>$data['cartoon_id']]);
        if (empty($cartoon)){
            return response()->json(['code'=>0,'msg'=>'当前用户不能添加此项目成员']);
        }
        $add['member_id'] =$to_member->member_id;
        $add['cartoon_id']=$data['cartoon_id'];
        $add['nickname']=$data['nickname'];
        $add['phone']=$data['phone'];

        $rs = $CartoonMembers->create($add);
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);
        }
    }
    /**
     * 修改项目成员资料
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveCartoonMembers(Request $request){
        $data = $request->all();
        $this->v->validate('saveCartoonMembers');
        $member_id=Auth::user()->member_id;
        $CartoonMembers=new CartoonMembers();
        $cartoon_id = $CartoonMembers->where(['id'=>$data['id']])->value('cartoon_id');
        if (empty($cartoon_id)){
            return response()->json(['code'=>0,'msg'=>'成员不存在']);
        }
        $CartoonMembers_c = $CartoonMembers->getData(['member_id'=>$member_id,'cartoon_id'=>$cartoon_id,'is_c'=>1]);
        $CartoonMembers_info = $CartoonMembers->getData(['member_id'=>$member_id,'id'=>$data['id']]);
        if (empty($CartoonMembers_info)&&empty($CartoonMembers_c)){
            return response()->json(['code'=>0,'msg'=>'您没有权限修改成员信息']);
        }
        $up['nickname']=$data['nickname'];
        $rs = $CartoonMembers->where(['id'=>$data['id']])->update($up);
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);

        }
    }
    /**
     * 删除项目成员
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delCartoonMembers(Request $request){
        $data = $request->all();
        $this->v->validate('delCartoonMembers');

        $member_id=Auth::user()->member_id;

        $CartoonMembers=new CartoonMembers();
        $cartoon_id = $CartoonMembers->where(['id'=>$data['id']])->value('cartoon_id');
//        if (empty($cartoon_id)){
//            return response()->json(['code'=>0,'msg'=>'成员不存在']);
//        }

        $CartoonMembers = $CartoonMembers->getData(['member_id'=>$member_id,'cartoon_id'=>$cartoon_id,'is_c'=>1]);
//        if (empty($CartoonMembers)){
//            return response()->json(['code'=>0,'msg'=>'您没有权限删除成员']);
//        }

        $rs = $CartoonMembers->where(['id'=>$data['id']])->delete();
        if ($rs){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);

        }
    }
    /**
     * 项目成员列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getCartoonMembers(Request $request){
        $data = $request->all();
        $this->v->validate('getCartoonMembers');

        $member_id=Auth::user()->member_id;

        $CartoonMembers=new CartoonMembers();
        $CartoonMembers_data = $CartoonMembers->getData(['member_id'=>$member_id,'cartoon_id'=>$data['cartoon_id']]);
        if (empty($CartoonMembers_data)){
            return response()->json(['code'=>0,'msg'=>'您没有权限查看成员']);
        }
        $CartoonMembers_list = $CartoonMembers->getList(['cartoon_id'=>$data['cartoon_id']],100);
        return response()->json(['code'=>1,'msg'=>'操作成功','data'=>$CartoonMembers_list['data']]);

    }

    /**
     * 添加项目漫画角色
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addCartoonRole(Request $request){
//        characters
        $this->v->validate('addCartoonRole');

        $member_id=Auth::user()->member_id;
        $roles=explode(',',$request->roles);
        $er='';
        if (!empty($roles)){
            foreach ($roles as $key=>$value){
                $CartoonRoles=new CartoonRoles();
                $to_CartoonRoles= $CartoonRoles->getData(['cartoon_id'=>$request->cartoon_id,'characters_id'=>$value]);
                if (!empty($to_CartoonRoles)){
                    $er=$er.$value.',';
                    continue;
                }
                $add['cartoon_id']=$request->cartoon_id;
                $add['characters_id']=$value;
                $add['member_id']=$member_id;
                $rs = $CartoonRoles->create($add);
            }
        }
        if (!empty($rs)){
            return response()->json(['code'=>1,'msg'=>'操作成功']);
        }else{
            return response()->json(['code'=>0,'msg'=>'操作失败']);
        }

    }







}
